package com.woniuxy.buyer.controller;

import com.woniuxy.buyer.entity.Product;
import com.woniuxy.buyer.service.ProductService;
import com.woniuxy.common.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品控制器
 */
@RestController
@RequestMapping("/products")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 获取所有在线商品
     */
    @GetMapping("/online")
    public Result<List<Product>> getOnlineProducts() {
        List<Product> products = productService.getOnlineProducts();
        return Result.success(products);
    }

    /**
     * 根据分类获取商品
     */
    @GetMapping("/category/{categoryId}")
    public Result<List<Product>> getProductsByCategory(@PathVariable Long categoryId) {
        List<Product> products = productService.getProductsByCategory(categoryId);
        return Result.success(products);
    }

    /**
     * 获取商品详情
     */
    @GetMapping("/{productId}")
    public Result<Product> getProductDetail(@PathVariable Long productId) {
        Product product = productService.getProductDetail(productId);
        if (product == null) {
            return Result.notFound("商品不存在或已下架");
        }
        return Result.success(product);
    }
}
